export const sellersListApi = () => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', 'http://127.0.0.1:3002/sellers/getAllSellers', true)
        xhr.send()
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}

export const searchSellersApi = sellersName => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', `http://127.0.0.1:3002/sellers/searchSellers?sellersName=${sellersName}`, true)
        xhr.send()
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}

export const delSellerNameApi = sid => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('DELETE', `http://127.0.0.1:3002/sellers/delSellers/${sid}`, true)
        xhr.send()
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}

export const addsellersApi = seller => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest
        xhr.open('POST', 'http://127.0.0.1:3002/sellers/addSellers', true)
        xhr.setRequestHeader('content-type', "application/json")
        xhr.send(seller)
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }

    })
}

export const fetchSellersListApi = id => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open("GET", `http://127.0.0.1:3002/sellers/sellersById/${id}`, true)
        xhr.send()
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}

export const modifySellersApi = seller => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('PUT', 'http://127.0.0.1:3002/sellers/modifySellers', true)
        xhr.setRequestHeader('content-type', "application/json")
        xhr.send(seller)
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}